<template>
  <div id="minutesPage" class="fixed top-0 left-0 right-0 bottom-0 bg-white-100">
    <Loading v-if="loading" />
    <!-- 密码输入 -->
    <PasswordVerifier v-else-if="showPSW" @verificationPassed="verificationPassed" />
    <!-- 通用错误信息 -->
    <ErrorView
      v-else-if="errorMsg.length > 0"
      :errorMessage="errorMsg"
      :needLogin="needLogin"
    />
    <!-- 主体内容 -->
    <MinutesLayout v-else />
  </div>
</template>

<script setup lang="ts" name="minutes">
import { onMounted, onBeforeMount, computed, ref } from "vue";
import { useMinutesStore } from "@/store/modules/minutes/minutes";
import { useRoute, useRouter } from "vue-router";
import Loading from "./components/loading/index.vue";
import MinutesLayout from "./components/minutesLayout/index.vue";
import PasswordVerifier from "./components/passwordVerifier/index.vue";
import ErrorView from "./components/commonError/ErrorView.vue";
import { useMinutes } from "./composables";
import { isPC } from "@/utils/platformTool";
import { useBreadcrumb } from "@/composables/useBreadcrumb";
import { getUserInfo } from "@/utils/auth";

const store = useMinutesStore();
const route = useRoute();
const router = useRouter();
const { checkMinutesUrl, pushToPcWeb } = useMinutes();

const {
  addBreadcrumb,
  addSystemBreadcrumb,
  addErrorBreadcrumb,
  reportBreadcrumbs,
  setupPageLifecycle,
} = useBreadcrumb();

onBeforeMount(() => {
  if (isPC()) {
    pushToPcWeb();
    return;
  }
});

onMounted(() => {
  reportBreadcrumbs(
    `打开纪要, ${JSON.stringify({
      url: window.location.href,
      userInfo: getUserInfo(),
    })}`
  );
  getData();
});

const errorDes = ref("");
const loading = ref(true);
async function getData() {
  const checkResult = checkMinutesUrl();
  if (checkResult.length > 0) {
    errorDes.value = `${checkResult}，请检查纪要链接是否正确!`;
    endLoading();
    return;
  }

  const { confId = "", shareId = "" } = route.params;
  store.conferenceId = confId as string;
  store.shareId = shareId as string;
  beganLoading();
  await store.fetchAllData();
  endLoading();
}

function beganLoading() {
  loading.value = true;
}

function endLoading() {
  loading.value = false;
}

const verificationPassed = () => {
  getData();
};

const showPSW = computed(() => {
  return store.needPassword;
});

const errorMsg = computed(() => {
  return store.errorMsg || errorDes.value;
});

const needLogin = computed(() => store.needLogin);
</script>

<style scoped></style>
